//
//  ios_Node.mm
//  Prism
//
//  Created by Bas du Pre on 21-05-11.
//  Copyright 2011 UU. All rights reserved.
//

#include "global.h"
#if PRISM_OS_IOS
#include "ios_global.h"

#include "Node.h"
#include "Director.h"
#include "CCNodePrism.h"

void Prism::Node::native_init() {
    IOS_node = 0;
    
    native_scale = [[CCDirector sharedDirector] contentScaleFactor];
    
    native_setCCNode([CCNode node]);
    
    onPositionChange += delegate(this, &Prism::Node::native_onPositionChange);
    onAnchorPointChange += delegate(this, &Prism::Node::native_onAnchorPointChange);
    onScaleChange += delegate(this, &Prism::Node::native_onScaleChange);
    onRotationChange += delegate(this, &Prism::Node::native_onRotationChange);
    onParentChange += delegate(this, &Prism::Node::native_onParentChange);
}

void Prism::Node::native_setCCNode(CCNode *node) {
    if (IOS_node != 0) [IOS_node unscheduleAllSelectors];
    [node unscheduleAllSelectors];
    IOS_node = node;
    [Prism::Director::instance()->native_currentScene() addChild:IOS_node];
    Prism::EventArgs e;
    
    native_onPositionChange(this, &e);
    native_onAnchorPointChange(this, &e);
    native_onScaleChange(this, &e);
    native_onRotationChange(this, &e);
    
    //[IOS_node performSelector:@selector(setPrismNode:) withObject:[NSValue valueWithPointer:this]];
    [IOS_node setPrismNode: [NSValue valueWithPointer:this]];
    [IOS_node schedule:@selector(prismUpdate:)];
}

void Prism::Node::native_onPositionChange(void* sender, Prism::EventArgs *e) {
    Prism::PointF position = native_correctPositionF(this->screenPosition());
    [IOS_node setPosition:ccp(position.x, position.y)];
    for (unsigned int i = 0; i < _childs.length(); i++) {
        _childs[i]->native_onPositionChange(sender, e);
    }
}

void Prism::Node::native_onAnchorPointChange(void* sender, Prism::EventArgs *e) {
    Prism::PointF anchorPoint = native_correctNormalizedPositionF(this->anchorPoint());
    [IOS_node setAnchorPoint:ccp(anchorPoint.x, anchorPoint.y)];
    for (unsigned int i = 0; i < _childs.length(); i++) {
        _childs[i]->native_onAnchorPointChange(sender, e);
    }
}

void Prism::Node::native_onScaleChange(void* sender, Prism::EventArgs *e) {
    [IOS_node setScaleX:screenScaleX() * native_scale];
    [IOS_node setScaleY:screenScaleY() * native_scale];
    for (unsigned int i = 0; i < _childs.length(); i++) {
        _childs[i]->native_onScaleChange(sender, e);
        _childs[i]->native_onPositionChange(sender, e);
    }
}

void Prism::Node::native_onRotationChange(void* sender, Prism::EventArgs *e) {
    [IOS_node setRotation:screenRotation()];
    for (unsigned int i = 0; i < _childs.length(); i++) {
        _childs[i]->native_onRotationChange(sender, e);
        _childs[i]->native_onPositionChange(sender, e);
    }
}

void Prism::Node::native_onParentChange(void* sender, Prism::EventArgs *e) {
    Prism::PointF position = native_correctPositionF(this->screenPosition());
    [IOS_node setPosition:ccp(position.x, position.y)];
    [IOS_node setScaleX:screenScaleX() * native_scale];
    [IOS_node setScaleY:screenScaleY() * native_scale];
    [IOS_node setRotation:screenRotation()];
    for (unsigned int i = 0; i < _childs.length(); i++) {
        _childs[i]->native_onParentChange(sender, e);
    }
}

void Prism::Node::native_visit() {
    [IOS_node visit];
}

bool Prism::Node::native_isRetinaNode() {
    return ([[CCDirector sharedDirector] contentScaleFactor] == 2 && native_scale == 1);
}

#endif